package com.tbit.uqbike.client.remote.nameauth;

import com.tbit.uqbike.client.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


/**
 * @author kang
 * @description:寻程实名认证工具类
 */
@Slf4j
public class ApiStoreAuthUtils {
    //通用url
    private static final String URL = "https://v.apistore.cn/api/a1";
    //大哈app_key
    private static final String DAHA_APP_KEY = "3bc97deaeb574c92ac0a2ac958d17808";
    //大哈app_key
    private static final String DM_APP_KEY = "f39c4f57e1f1b8574b210e0520818efa";
    //大哈app_key
    private static final String DD_APP_KEY = "4bb4ae0f06b9782bc7266c501fcbc067";

    /**
     * 用户事情认证
     *
     * @param name  姓名
     * @param carNo 身份证号码
     */
    public static Boolean authDaHa(String name, String cardNo) {
        //请求头
        Map<String, Object> headers = new HashMap<>();
        headers.put("Accept-Charset", "utf-8");
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", DAHA_APP_KEY);
        params.put("cardNo", cardNo);
        params.put("realName", name);
        params.put("information", "0");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));//使用https连接池，导致jar包冲突，这里用apiStore提供的方式
        System.out.println(json);
        if (json != null) {
            ApiStoreAuthResult result = JsonUtils.json2Obj(json, ApiStoreAuthResult.class);
            if (result.getError_code() != 0) {
                log.info("[大哈]寻程实名认证接口调用失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 电马实名认证
     *
     * @param name   姓名
     * @param cardNo 身份证号码
     */
    public static Boolean authDM(String name, String cardNo) {
        //请求头
        Map<String, Object> headers = new HashMap<>();
        headers.put("Accept-Charset", "utf-8");
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", DM_APP_KEY);
        params.put("cardNo", cardNo);
        params.put("realName", name);
        params.put("information", "0");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));//使用https连接池，导致jar包冲突，这里用apiStore提供的方式
        System.out.println(json);
        if (json != null) {
            ApiStoreAuthResult result = JsonUtils.json2Obj(json, ApiStoreAuthResult.class);
            if (result.getError_code() != 0) {
                log.info("[电马]寻程实名认证接口调用失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 迪迪实名认证
     *
     * @param name   姓名
     * @param cardNo 身份证号码
     */
    public static Boolean authDD(String name, String cardNo) {
        //请求头
        Map<String, Object> headers = new HashMap<>();
        headers.put("Accept-Charset", "utf-8");
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", DD_APP_KEY);
        params.put("cardNo", cardNo);
        params.put("realName", name);
        params.put("information", "0");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));//使用https连接池，导致jar包冲突，这里用apiStore提供的方式
        System.out.println(json);
        if (json != null) {
            ApiStoreAuthResult result = JsonUtils.json2Obj(json, ApiStoreAuthResult.class);
            if (result.getError_code() != 0) {
                log.info("[电马]寻程实名认证接口调用失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        authDaHa("李卫康", "130527199304282017");
    }
}
